<style lang="less">
  @import './login.less';
</style>

<template>
  <div class="login">
    <div class="login-con">
      <Card icon="log-in" title="欢迎登录" :bordered="false">
        <div class="form-con">
          <login-form @on-success-valid="handleSubmit"></login-form>
          <p class="login-tip">输入任意用户名和密码即可</p>
        </div>
      </Card>
    </div>
  </div>
</template>

<script>
import LoginForm from '_c/login-form'
import { mapActions } from 'vuex'
export default {
  components: {
    LoginForm
  },
  beforeMount () {
    if (this.$storage.session.get('sysUser') != null) { // 代表已经登录过了
      this.$router.push({
        name: 'home'
      })
    }
  },
  methods: {
    ...mapActions([
      'handleLogin',
      'getUserInfo'
    ]),
    handleSubmit ({ username, password }) {
      console.log({ username, password })
      this.$Message.info('登录中!')
      this.$api.sysUser.loginUser({ username, password }).then(res => {
        console.log(res)
        if (res.status === 200) {
          if (res.data.data) {
            this.$Message.success('登录成功')
            this.$storage.session.set('sysUser', res.data.data) // 将这个用户信息设置到本地缓存
            this.$router.push({
              name: 'home'
            })
          } else {
            this.$Message.error('登录失败请检查用户名或密码是否正确!')
          }
        }
      }).catch(err => {
        console.log(err)
      })

      // this.handleLogin({ userName, password }).then(res => {
      //   this.getUserInfo().then(res => {
      //     this.$router.push({
      //       name: this.$config.homeName
      //     })
      //   })
      // })
    }
  }
}
</script>

<style>

</style>
